package com.zhuge_new.Activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhuge_new.Adapter.MinDepositAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.R;
import com.zhuge_new.bean.MinBean;
import com.zhuge_new.bean.MinDepositBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/11/13.
 */

public class DepositActivity extends BaseActivity implements View.OnClickListener {

    private SwipeMenuRecyclerView recycle_list;
    private ImageView deposit_back;
    private TextView deposit_text;
    private MinDepositAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit);
        init_title();
        getData();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText("提现记录");
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        deposit_back = findViewById(R.id.deposit_back);
        deposit_text = findViewById(R.id.deposit_text);
        iv_back.setOnClickListener(this);
        recycle_list = findViewById(R.id.recycle_list);
        recycle_list.setLayoutManager(new LinearLayoutManager(this));
        recycle_list.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(DepositActivity.class);
                break;
        }
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.getWithdrawList)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("openid", PreferencesUtils.getString(baseContext, "unionid"))
                .params("page", pageNum)
                .params("rows", "15")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body());
                            String object = obj.getString("object");
                            if (object.isEmpty()) {

                            } else {
                                deposit_text.setVisibility(View.GONE);
                                deposit_back.setVisibility(View.GONE);
                                MinDepositBean minDepositBean = (MinDepositBean) new DataParse().getBean(response.body(), MinDepositBean.class, 0);
                                adapter = new MinDepositAdapter(baseContext, minDepositBean.getObject());
                                if (minDepositBean.getObject().size() < 15) {

                                } else {
                                    adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                        @Override
                                        public void onLoadMoreRequested() {
                                            loadMore();
                                        }
                                    }, recycle_list);
                                }
                                recycle_list.setAdapter(adapter);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void loadMore() {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.getWithdrawList)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("openid", PreferencesUtils.getString(baseContext, "unionid"))
                .params("page", pageNum)
                .params("rows", "15")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {

                        MinDepositBean minDepositBean = (MinDepositBean) new DataParse().getBean(response.body(), MinDepositBean.class, 0);
                        adapter.addData(minDepositBean.getObject());
                        adapter.loadMoreComplete();
                        if (minDepositBean.getObject().isEmpty()) {
                            pageNum = pageNum - 1;
                            adapter.setEnableLoadMore(false);
                        }
                    }
                });
    }
}
